// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UcItemDetailsContainer.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The uc item details container.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.WinClient.Forms.DbItems
{
    using System.Data;
    using Common.Helper;
    using Common.Model;

    /// <summary>
    /// The uc item details container.
    /// </summary>
    public class UcItemDetailsContainer : UcDbItem
    {
        #region Fields & properties

        /// <summary>
        /// The _ schema name.
        /// </summary>
        private string _SchemaName;

        /// <summary>
        /// The _item type.
        /// </summary>
        private DatabaseObjectTypes _itemType;

        /// <summary>
        /// The _object name.
        /// </summary>
        private string _objectName;

        /// <summary>
        /// Gets ItemType.
        /// </summary>
        public DatabaseObjectTypes ItemType
        {
            get { return this._itemType; }
        }

        #endregion Fields & properties

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UcItemDetailsContainer"/> class.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="itemType">
        /// The item type.
        /// </param>
        public UcItemDetailsContainer(DatabaseInfo data, DatabaseObjectTypes itemType)
        {
            this.InitializeComponent();
            this.Data = data;
            this._itemType = itemType;
            this._objectName = string.Empty;
            this._SchemaName = string.Empty;

            this.Init();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UcItemDetailsContainer"/> class.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="itemType">
        /// The item type.
        /// </param>
        /// <param name="SchemaName">
        /// The schema name.
        /// </param>
        /// <param name="objectName">
        /// The object name.
        /// </param>
        public UcItemDetailsContainer(DatabaseInfo data, DatabaseObjectTypes itemType, string SchemaName, 
                                      string objectName)
        {
            this.InitializeComponent();
            this.Data = data;
            this._itemType = itemType;
            this._objectName = objectName;
            this._SchemaName = SchemaName;

            this.Init();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UcItemDetailsContainer"/> class.
        /// </summary>
        /// <param name="itemType">
        /// The item type.
        /// </param>
        public UcItemDetailsContainer(DatabaseObjectTypes itemType)
        {
            this.InitializeComponent();
            this._itemType = itemType;
            this._objectName = string.Empty;
            this._SchemaName = string.Empty;

            this.Init();
        }

        /// <summary>
        /// The init method
        /// </summary>
        private void Init()
        {
            this.HeaderVisible = true;

            DataRow[] rows;
            switch (this._itemType)
            {
                case DatabaseObjectTypes.Table:
                    rows =
                        ((DatabaseInfo)this.Data).GetRawDatatable(DatabaseObjectTypes.Table).Select(string.Format("TableName='{0}' AND SchemaName='{1}'", this._objectName, this._SchemaName));

                    if ((this.DetailsControl != null) && (this.DetailsControl is UcTableInfo))
                    {
                        ((UcTableInfo)this.DetailsControl).Init(rows.Length > 0 ? rows[0] : null, (DatabaseInfo)this.Data, false);
                    }
                    else
                    {
                        this.DetailsControl = rows.Length > 0 ? new UcTableInfo(rows[0], (DatabaseInfo)this.Data, false) : new UcTableInfo(null, (DatabaseInfo)this.Data, false);
                    }

                    break;

                case DatabaseObjectTypes.Procedure:
                case DatabaseObjectTypes.Function:
                case DatabaseObjectTypes.Trigger:
                case DatabaseObjectTypes.View:
                case DatabaseObjectTypes.Rule:
                case DatabaseObjectTypes.Default:
                    rows = ((DatabaseInfo)this.Data).GetRawDatatable(this._itemType).Select(string.Format("Name='{0}' AND SchemaName='{1}'", this._objectName, this._SchemaName));
                    if ((this.DetailsControl != null) && (this.DetailsControl is UcExecInfo))
                    {
                        ((UcExecInfo)this.DetailsControl).Init(rows.Length > 0 ? rows[0] : null, (DatabaseInfo)this.Data, false, this._itemType);
                    }
                    else
                    {
                        this.DetailsControl = rows.Length > 0
                                                  ? new UcExecInfo(rows[0], (DatabaseInfo)this.Data, false, this._itemType)
                                                  : new UcExecInfo(null, (DatabaseInfo)this.Data, false, this._itemType);
                    }

                    break;

                case DatabaseObjectTypes.User:
                    rows =
                        ((DatabaseInfo)this.Data).GetRawDatatable(DatabaseObjectTypes.User).Select(string.Format("Name='{0}'", this._objectName));
                    if ((this.DetailsControl != null) && (this.DetailsControl is UcExec))
                    {
                        ((UcExecInfo)this.DetailsControl).Init(rows.Length > 0 ? rows[0] : null, (DatabaseInfo)this.Data, false, this._itemType);
                    }
                    else
                    {
                        this.DetailsControl = rows.Length > 0
                                                  ? new UcExecInfo(rows[0], (DatabaseInfo)this.Data, false, this._itemType)
                                                  : new UcExecInfo(null, (DatabaseInfo)this.Data, false, this._itemType);
                    }

                    break;
            }

            switch (this._itemType)
            {
                case DatabaseObjectTypes.Table:
                    this.Title = "Table properties";
                    break;
                case DatabaseObjectTypes.Procedure:
                    this.Title = "Stored procedure properties";
                    break;
                case DatabaseObjectTypes.Function:
                    this.Title = "Function properties";
                    break;
                case DatabaseObjectTypes.Trigger:
                    this.Title = "Trigger properties";
                    break;
                case DatabaseObjectTypes.View:
                    this.Title = "View properties";
                    break;
                case DatabaseObjectTypes.User:
                    this.Title = "User properties";
                    break;
                case DatabaseObjectTypes.Rule:
                    this.Title = "Rule properties";
                    break;
                case DatabaseObjectTypes.Default:
                    this.Title = "Default properties";
                    break;
                    // case dbObjectsTable.tblRole:
                    // title = "Role properties";
                    // break;
                case DatabaseObjectTypes.UDT:
                    this.Title = "User defined type properties";
                    break;
                default:
                    this.Title = string.Empty;
                    break;
            }

            this.Title = string.Format("[{0}].[{1}] {2}", this._SchemaName, this._objectName, this.Title);
            this.Toolbar = null;
            this.CommandMenu = null;
        }

        #endregion Constructors

        #region Form view / data update

        /// <summary>
        /// The update form data.
        /// </summary>
        protected override void UpdateFormData()
        {
            this.LblHeader.Text = this.Title;
        }

        #endregion Form view / data update

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="itemType">
        /// The item type.
        /// </param>
        /// <param name="SchemaName">
        /// The schema name.
        /// </param>
        /// <param name="objectName">
        /// The object name.
        /// </param>
        public void SetData(DatabaseInfo data, DatabaseObjectTypes itemType, string SchemaName, string objectName)
        {
            this.Data = data;
            this._itemType = itemType;
            this._objectName = objectName;
            this._SchemaName = SchemaName;

            this.Init();
        }

        #region Designer generated code

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        ///   the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion
    }
}